package cn.edu.zjut.mina.po;

import java.util.Arrays;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;

import cn.edu.zjut.mina.bean.Card;
import cn.edu.zjut.mina.bean.PlayerInfo;
import cn.edu.zjut.mina.bean.PlayingCards;


/**
 * 用户信息类
 * 当用户登入的时候
 * @author 张宇翔
 */
@Table(name="t_userInfo")
@Entity
public class UserInfo {
	private Integer autoid;
	private String nickName;//昵称
	private String unionid;
	private String openid;//普通用户的标识
	private String refresh_token;
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date refresh_token_time;//refresh_token的刷新时间
	private String expires_in;//access_token接口调用凭证超时时间
	private String access_token;
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date access_token_time;//access_token的刷新时间
	private String sex;
	private String province;
	private String city;
	private String country;
	@Column(nullable=false, columnDefinition="varchar(50)")
	private String userId;//用户ID   (即微信号)
	private String headimgurl;//头像地址
	private Integer roomCardNum;//房卡数
	private Integer lostConnection=0;//是否掉线  0  未掉线   1 掉线
	
	
	//不保存   (以下信息使得一个用户变成一个玩家)
	private String roomId;//房间号
	private Integer credits=0;//积分
	private String ip;//用户的IP地址   用于检测是否有同IP的用户
	private int port;//用户的端口号   用户处理异地登录
	private Integer position;//座位号
	private Integer prepare=0;//1   准备    0  未准备
	private Integer showCards=0;//1  亮牌  0  未亮牌
	private Integer sendMessage;//1  发送消息  0  未发送消息
	private String message;//消息内容
	private Integer quit;//退出       1   退出   0   不退出
	private Integer backtohall;//返回大厅       1   返回大厅      0   返回房间   
	
	private Card[] cards;//玩家牌信息
	private Integer bet;//下注(我理解为底分)   现在由于底分为1，所以暂时没有用
	private Boolean isBanker;//是否是庄家
	private Integer mBetRatio;//压注倍率   1，2，3
	private Boolean hasNiuNiu;//是否有牛
	private Integer[] mNiuNiuPosition;//牛牛所在的位置
	private Double coins;//玩家现有金币数 (暂时没用)
	private Integer maxCardsNum;//最大的牌的大小
	private Integer maxCardsColor;//最大牌的花色   0 方块      1 草花      2  红桃    3黑桃
	private Integer residueSum;//剩余数和
	private Boolean fiveTen=false;//是否拥有5张10
	private Integer mWinRatio;//赢得的倍率
	private Integer mCountOfNiuNiu=0;//玩家一次对局中出现牛牛的次数
	private Integer mCountOfYouNiu=0;//玩家一次对局中出现有牛的次数
	private Integer mCountOfWuHua=0;//玩家一次对局中出现五花的次数
	private Boolean mLastRoundPoNiu=false;//玩家上一局是否破牛  (在破牛庄的时候有用)   ture 表示破牛了              false表示没有破牛
	private Date lastOperationTime;//上一次操作时间
	private PlayerInfo playerInfo;
	
	//初始化玩家信息
	public void initPlayerInfo(){
		playerInfo=new PlayerInfo();
		playerInfo.secretGang=0;//暗杠次数
		playerInfo.bareGang=0;//明杠次数
		playerInfo.pengTimes=0;//碰次数
		playerInfo.huTimes=0;//胡
		playerInfo.dianPao=0;//点炮
		playerInfo.jiePao=0;//接炮
		playerInfo.zimoTimes=0;//自摸
		playerInfo.score=0;//分数
	}
	@Transient
	public PlayerInfo getPlayerInfo(){
		return playerInfo;
	}
//	public UserInfo() {
//		
//		this.mCountOfNiuNiu = 0;
//		this.mCountOfYouNiu = 0;
//		this.mCountOfWuHua = 0;
//		this.mLastRoundPoNiu = false;
//		this.lostConnection = 0;
//		this.prepare = 0;
//		this.showCards = 0;
//	}	
	
	
	@GeneratedValue
	@Id
	public Integer getAutoid() {
		return autoid;
	}
	public void setAutoid(Integer autoid) {
		this.autoid = autoid;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public Integer getRoomCardNum() {
		return roomCardNum;
	}
	public void setRoomCardNum(Integer roomCardNum) {
		this.roomCardNum = roomCardNum;
	}
	public String getUnionid() {
		return unionid;
	}
	public void setUnionid(String unionid) {
		this.unionid = unionid;
	}
	public String getOpenid() {
		return openid;
	}
	public void setOpenid(String openid) {
		this.openid = openid;
	}
	public String getRefresh_token() {
		return refresh_token;
	}
	public void setRefresh_token(String refresh_token) {
		this.refresh_token = refresh_token;
	}
	public Date getRefresh_token_time() {
		return refresh_token_time;
	}
	public void setRefresh_token_time(Date refresh_token_time) {
		this.refresh_token_time = refresh_token_time;
	}
	public String getExpires_in() {
		return expires_in;
	}
	public void setExpires_in(String expires_in) {
		this.expires_in = expires_in;
	}
	public String getAccess_token() {
		return access_token;
	}
	public void setAccess_token(String access_token) {
		this.access_token = access_token;
	}
	public Date getAccess_token_time() {
		return access_token_time;
	}
	public void setAccess_token_time(Date access_token_time) {
		this.access_token_time = access_token_time;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getHeadimgurl() {
		return headimgurl;
	}
	public void setHeadimgurl(String headimgurl) {
		this.headimgurl = headimgurl;
	}
	
	@Transient
	public Integer getCredits() {
		return credits;
	}
	public void setCredits(Integer credits) {
		this.credits = credits;
	}
	@Transient
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	@Transient
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	@Transient
	public Integer getPosition() {
		return position;
	}
	public void setPosition(Integer position) {
		this.position = position;
	}
	@Transient
	public Integer getPrepare() {
		return prepare;
	}
	public void setPrepare(Integer prepare) {
		this.prepare = prepare;
	}
	@Transient
	public Card[] getCards() {
		return cards;
	}
	public void setCards(Card[] cards) {
		this.cards = cards;
	}
	@Transient
	public Integer getBet() {
		return bet;
	}
	public void setBet(Integer bet) {
		this.bet = bet;
	}
	@Transient
	public Boolean isBanker() {
		return isBanker;
	}
	public void setBanker(Boolean isBanker) {
		this.isBanker = isBanker;
	}
	@Transient
	public Integer getmBetRatio() {
		return mBetRatio;
	}
	public void setmBetRatio(Integer mBetRatio) {
		this.mBetRatio = mBetRatio;
	}
	@Transient
	public Boolean isHasNiuNiu() {
		return hasNiuNiu;
	}
	public void setHasNiuNiu(Boolean hasNiuNiu) {
		this.hasNiuNiu = hasNiuNiu;
	}
	@Transient
	public Integer[] getmNiuNiuPosition() {
		return mNiuNiuPosition;
	}
	public void setmNiuNiuPosition(Integer[] mNiuNiuPosition) {
		this.mNiuNiuPosition = mNiuNiuPosition;
	}
	@Transient
	public Double getCoins() {
		return coins;
	}
	public void setCoins(Double coins) {
		this.coins = coins;
	}
	@Transient
	public Integer getMaxCardsNum() {
		return maxCardsNum;
	}
	public void setMaxCardsNum(Integer maxCardsNum) {
		this.maxCardsNum = maxCardsNum;
	}
	@Transient
	public Integer getMaxCardsColor() {
		return maxCardsColor;
	}
	public void setMaxCardsColor(Integer maxCardsColor) {
		this.maxCardsColor = maxCardsColor;
	}
	@Transient
	public Integer getResidueSum() {
		return residueSum;
	}
	public void setResidueSum(Integer residueSum) {
		this.residueSum = residueSum;
	}
	@Transient
	public Boolean isFiveTen() {
		return fiveTen;
	}
	public void setFiveTen(Boolean fiveTen) {
		this.fiveTen = fiveTen;
	}
	@Transient
	public Integer getmWinRatio() {
		return mWinRatio;
	}
	public void setmWinRatio(Integer mWinRatio) {
		this.mWinRatio = mWinRatio;
	}
	@Transient
	public Integer getmCountOfNiuNiu() {
		return mCountOfNiuNiu;
	}
	public void setmCountOfNiuNiu(Integer mCountOfNiuNiu) {
		this.mCountOfNiuNiu = mCountOfNiuNiu;
	}
	@Transient
	public Integer getmCountOfYouNiu() {
		return mCountOfYouNiu;
	}
	public void setmCountOfYouNiu(Integer mCountOfYouNiu) {
		this.mCountOfYouNiu = mCountOfYouNiu;
	}
	@Transient
	public Integer getmCountOfWuHua() {
		return mCountOfWuHua;
	}
	public void setmCountOfWuHua(Integer mCountOfWuHua) {
		this.mCountOfWuHua = mCountOfWuHua;
	}
	@Transient
	public Boolean ismLastRoundPoNiu() {
		if (mLastRoundPoNiu == null) {
			mLastRoundPoNiu = false;
		}
		System.out.println("mLastRoundPoNiu = " + mLastRoundPoNiu + " ==============");
		return mLastRoundPoNiu;
	}
	public void setmLastRoundPoNiu(Boolean mLastRoundPoNiu) {
		this.mLastRoundPoNiu = mLastRoundPoNiu;
	}
	
	public void getConcreteCards() {
		for(int i = 0; i < cards.length; i++) {
			if (cards[i].getColor() == 0) {
				System.out.print("方块" + cards[i].getNumber() + " ");
			} else if (cards[i].getColor() == 1) {
				System.out.print("草花" + cards[i].getNumber() + " ");
			} else if (cards[i].getColor() == 2) {
				System.out.print("红桃" + cards[i].getNumber() + " ");
			} else if (cards[i].getColor() == 3) {
				System.out.print("黑桃" + cards[i].getNumber() + " ");
			}
		}
		System.out.println();//换行
	}
	
	public void computeNiuNiu() {
		if (mNiuNiuPosition == null) {
			mNiuNiuPosition = new Integer[3];
		}
		if (mCountOfYouNiu == null) {
			mCountOfYouNiu = 0;
		}
		if (mCountOfWuHua == null) {
			mCountOfWuHua = 0;
		}
		if (mCountOfNiuNiu == null) {
			mCountOfNiuNiu = 0;
		}
		if (mLastRoundPoNiu == null) {
			mLastRoundPoNiu = false;
		}
		/**
		 * 计算是否有牛牛与牛牛的位置
		 */
		for (int i = 0; i < 3; i++) {
			if (hasNiuNiu == true)
				break;
			for (int j = i + 1; j < 4; j++) {
				if (hasNiuNiu == true)
					break;
				for (int k = j + 1; k < 5; k++) {
					if (hasNiuNiu == true)
						break;
					int digit1 = cards[i].getNumber() > 10?10:cards[i].getNumber();//J,Q,K都是当10计算
					int digit2 = cards[j].getNumber() > 10?10:cards[j].getNumber();
					int digit3 = cards[k].getNumber() > 10?10:cards[k].getNumber();
					int sum = digit1 + digit2 + digit3;
					if (sum % 10 == 0) {
						mNiuNiuPosition[0] = i;
						mNiuNiuPosition[1] = j;
						mNiuNiuPosition[2] = k;
						hasNiuNiu = true;
					}
				}
			}
		}
		/**
		 * 在有牛的情况下，判断是不是有5个10
		 */
		if (hasNiuNiu == true) {
			System.out.println("有牛");
			System.out.println("下标位置    " + mNiuNiuPosition[0] + " " + mNiuNiuPosition[1] + " " + mNiuNiuPosition[2]);
			mCountOfYouNiu ++;//有牛次数加一
			int tempFiveTen = 0;//0 表示5个10   1表示存在至少一张牌点数小于10
			for (int i = 0; i < PlayingCards.NUMBER_OF_CARDS; i++) {
				if (cards[i].getNumber() < 10) {
					tempFiveTen = 1;
					break;
				}	
			}
			
			if (tempFiveTen == 0) {
				setFiveTen(true);
				mCountOfWuHua ++;//五花次数加一
			}
		}
		
		/**
		 * 拷贝牌的副本用于排序
		 * 不管有没有牛都要获取最大牌
		 */
		Card[] tempCards = new Card[5];
		for (int i = 0; i < PlayingCards.NUMBER_OF_CARDS; i++) {
			tempCards[i] = cards[i];
		}
		Arrays.sort(tempCards);//排序
//		for(int i = 0; i < tempCards.length; i++) {
//			System.out.print(tempCards[i].getColor() + " " + tempCards[i].getNumber() + " ");
//		}
//		System.out.println();
		setMaxCardsNum(tempCards[0].getNumber());//获取最大牌的点数
		setMaxCardsColor(tempCards[0].getColor());//获取最大牌的颜色
//		System.out.println(maxCardsNum + " " + maxCardsColor);
		tempCards = null;//释放资源
	}
	
	public void computeResidueSum () {//计算剩余和
		for (int i = 0; i < PlayingCards.NUMBER_OF_CARDS; i++) {
			if (i != mNiuNiuPosition[0] && i != mNiuNiuPosition[1] && i != mNiuNiuPosition[2]) {
				residueSum += cards[i].getNumber() > 10?10:cards[i].getNumber();
			}
		}
		if (!fiveTen && residueSum % 10 == 0) {
			mCountOfNiuNiu ++;//牛牛次数加一
		}
		System.out.println("residueSum(也就是M) " + residueSum);
	}
	
	public void reSet () {//重置其中一些变量
		cards = null;
		isBanker = false;//是否是庄家
		mBetRatio = 1;//压注倍率
		hasNiuNiu = false;//是否有牛
		maxCardsNum = 0;//最大的牌的大小
		maxCardsColor = 0;//最大牌的花色   0 方块      1 草花      2  红桃    3黑桃
		residueSum = 0;//剩余数和
		fiveTen = false;//是否拥有5张10
		mWinRatio = 0;//赢得的倍率
	}
	@Transient
	public Integer getShowCards() {
		return showCards;
	}
	public void setShowCards(Integer showCards) {
		this.showCards = showCards;
	}
	@Transient
	public String getRoomId() {
		return roomId;
	}
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}
	@Transient
	public Date getLastOperationTime() {
		return lastOperationTime;
	}
	public void setLastOperationTime(Date lastOperationTime) {
		this.lastOperationTime = lastOperationTime;
	}
	@Transient
	public Integer getSendMessage() {
		return sendMessage;
	}
	public void setSendMessage(Integer sendMessage) {
		this.sendMessage = sendMessage;
	}
	@Transient
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Integer getLostConnection() {
		return lostConnection;
	}
	public void setLostConnection(Integer lostConnection) {
		this.lostConnection = lostConnection;
	}
	
	@Override
	public String toString() {
		return "UserInfo [autoid=" + autoid + ", nickName=" + nickName
				+ ", unionid=" + unionid + ", openid=" + openid
				+ ", refresh_token=" + refresh_token + ", refresh_token_time="
				+ refresh_token_time + ", expires_in=" + expires_in
				+ ", access_token=" + access_token + ", access_token_time="
				+ access_token_time + ", sex=" + sex + ", province=" + province
				+ ", city=" + city + ", country=" + country + ", userId="
				+ userId + ", headimgurl=" + headimgurl + ", roomCardNum="
				+ roomCardNum + ", lostConnection=" + lostConnection
				+ ", roomId=" + roomId + ", credits=" + credits + ", ip=" + ip
				+ ", position=" + position + ", prepare=" + prepare
				+ ", showCards=" + showCards + ", sendMessage=" + sendMessage
				+ ", message=" + message + ", cards=" + Arrays.toString(cards)
				+ ", bet=" + bet + ", isBanker=" + isBanker + ", mBetRatio="
				+ mBetRatio + ", hasNiuNiu=" + hasNiuNiu + ", mNiuNiuPosition="
				+ Arrays.toString(mNiuNiuPosition) + ", coins=" + coins
				+ ", maxCardsNum=" + maxCardsNum + ", maxCardsColor="
				+ maxCardsColor + ", residueSum=" + residueSum + ", fiveTen="
				+ fiveTen + ", mWinRatio=" + mWinRatio + ", mCountOfNiuNiu="
				+ mCountOfNiuNiu + ", mCountOfYouNiu=" + mCountOfYouNiu
				+ ", mCountOfWuHua=" + mCountOfWuHua + ", mLastRoundPoNiu="
				+ mLastRoundPoNiu + ", lastOperationTime=" + lastOperationTime
				+ "]";
	}
	@Transient
	public Integer getQuit() {
		return quit;
	}
	public void setQuit(Integer quit) {
		this.quit = quit;
	}
	@Transient
	public Integer getBacktohall() {
		return backtohall;
	}
	public void setBacktohall(Integer backtohall) {
		this.backtohall = backtohall;
	}
	
	public void clear() {//清空玩家信息，使其重新变成一个用户
		this.roomId = null;
		this.lostConnection = 0;
		this.credits = 0;
		this.position = null;
		this.prepare = 0;
		this.showCards = 0;
		this.sendMessage = null;
		this.message = null;
		this.quit = null;
		this.backtohall = null;
		this.cards = null;
		this.bet = null;
		this.isBanker = null;
		this.mBetRatio = null;
		this.hasNiuNiu = null;
		this.mNiuNiuPosition = null;
		this.coins = null;
		this.maxCardsNum = null;
		this.maxCardsColor = null;
		this.residueSum = null;
		this.fiveTen = false;
		this.mWinRatio = null;
		this.mCountOfNiuNiu = 0;
		this.mCountOfWuHua = 0;
		this.mCountOfYouNiu = 0;
		this.mLastRoundPoNiu = false;
		this.lastOperationTime = null;
	}
}
